var app = getApp();

Page({
  data: {
    telephone: 0,
    codeBtnText: '获取验证码',
    sec: 60,
    phoneCode: 0
  },
  onLoad: function() {
    // wx.removeStorageSync("telephone");
    this.wxLogin();
    var hasLogin = wx.getStorageSync('isLogined');
    if (hasLogin) {
      wx.redirectTo({
        url: "../index"
      })
    }
  },
  // 获取手机号码
  getPhoneNumber: function(event) {
    var val = event.detail.value;
    this.setData({
      telephone: val
    });
  },
  // 获取验证码
  sendPhoneCode: function(event) {
    var that = this;
    var thisVal = event.currentTarget.dataset.text;
    var telephone = this.data.telephone;
    var myreg = /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$$/;
    if (myreg.test(telephone)) {
      if (thisVal == '获取验证码') {
        var remain = 60; //用另外一个变量来操作秒数是为了保存最初定义的倒计时秒数，就不用在计时完之后再手动设置秒数
        var time = setInterval(function() {
          if (remain == 1) {
            clearInterval(time);
            that.setData({
              sec: 60,
              codeBtnText: '获取验证码'
            })
            return false //必须有
          }
          remain--;
          that.setData({
            sec: remain,
            codeBtnText: that.data.sec + 's重新获取'
          })
        }, 1000);

        wx.request({
          url: app.globalData.apiBaseUrl + '/wechat/sendMessage',
          header: {
            'content-type': 'application/json; charset=utf-8'
          },
          method: 'GET',
          data: {
            telephone: that.data.telephone
          },
          dataType: 'json',
          success: function(res) {
            res = res.data;
            if (res.code == 200) {
              that.setData({
                phoneCode: res.data
              })
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 3000
              })
            }
          }
        })
      }
    } else {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 3000
      })
    }
  },
  // 提交表单-绑定手机号
  formSubmitPhone: function(e) {
    var that = this;
    var formObj = e.detail.value;

    var myreg = /^(13[0-9]|14[0-9]|15[0-9]|17[0-9]|18[0-9]|19[0-9])\d{8}$$/;
    if (myreg.test(formObj.telephone) && formObj.telephone != '' && formObj.phoneCode != '') {
      if (formObj.phoneCode !== this.data.phoneCode) {
        wx.showToast({
          title: '请填写正确的短信验证码',
          icon: 'none',
          duration: 2000
        })
      } else {
        wx.request({
          url: app.globalData.apiBaseUrl + '/wechat/wxUserLogin',
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          data: {
            openId: wx.getStorageSync("openid"),
            tel: formObj.telephone
          },
          dataType: 'json',
          success: function(res) {
            res = res.data;
            if (res.code == 200) {
              wx.setStorageSync('telephone', formObj.telephone);
              wx.setStorageSync('token', res.data.token);
              wx.setStorageSync('isLogined', true);
              var age = Number(res.data.age);
              if (age > 15) {
                wx.setStorageSync("isChild", false)
              } else {
                wx.setStorageSync("isChild", true)
              }
              var ifHave = res.data.ifHave;
              if (ifHave == 0) {
                wx.redirectTo({
                  url: "../baseInfo/baseInfo"
                })
              } else {
                wx.redirectTo({
                  url: "../index"
                })
              }
            } else {
              wx.showToast({
                title: res.msg,
                icon: 'none',
                duration: 3000
              })
            }
          },
          fail: function(err) {
            wx.showToast({
              title: err,
              icon: 'none',
              duration: 3000
            })
          }
        })
      }
    } else {
      wx.showToast({
        title: '请完善填写信息',
        icon: 'none',
        duration: 3000
      })
    }

    // wx.request({
    //   url: app.globalData.apiBaseUrl + '/wechat/wxUserLogin',
    //   header: {
    //     'content-type': 'application/x-www-form-urlencoded'
    //   },
    //   method: 'POST',
    //   data: {
    //     openId: wx.getStorageSync("openid"),
    //     tel: formObj.telephone
    //   },
    //   dataType: 'json',
    //   success: function (res) {
    //     res = res.data;
    //     if (res.code == 200) {
    //       wx.setStorageSync('telephone', formObj.telephone);
    //       wx.setStorageSync('token', res.data.token);
    //       wx.setStorageSync('isLogined', true);
    //       var age = Number(res.data.age);
    //       if (age > 15) {
    //         wx.setStorageSync("isChild", false)
    //       } else {
    //         wx.setStorageSync("isChild", true)
    //       }
    //       var ifHave = res.data.ifHave;
    //       if (ifHave == 0) {
    //         wx.redirectTo({
    //           url: "../baseInfo/baseInfo"
    //         })
    //       } else {
    //         wx.redirectTo({
    //           url: "../index"
    //         })
    //       }
    //     } else {
    //       wx.showToast({
    //         title: res.msg,
    //         icon: 'none',
    //         duration: 3000
    //       })
    //     }
    //   },
    //   fail: function (err) {
    //     wx.showToast({
    //       title: err,
    //       icon: 'none',
    //       duration: 3000
    //     })
    //   }
    // })
    
  },
  wxLogin: function() {
    var userInfo = wx.getStorageSync("userInfo");
    wx.login({
      success(res) {
        if (res.code) {
          wx.request({
            url: app.globalData.apiBaseUrl + '/wechat/giveAuth',
            data: {
              code: res.code,
              rawData: userInfo.rawData
            },
            success: function(res) {
              wx.setStorageSync('openid', res.data.openid);
            }
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
})